import os
import random
from math import sqrt

# game constants
from constants import *

# cocos componets used by this scene
from cocos.director import director
from cocos.scene import Scene
from cocos.layer import Layer, ColorLayer
from cocos.scenes.transitions import *
from cocos.actions import *
from cocos.sprite import Sprite
from cocos.text import Label
from cocos.cocosnode import CocosNode
from cocos.batch import BatchNode
from cocos.draw import Line

# pyglet  componets used by this scene
import pyglet
from pyglet import gl, font
from pyglet.window import key
from pyglet.gl import *


# this class prints out all the informations on a given planet
# in a nice side panel
class PanelLayer(Layer):
    
    is_event_handler = True     #: enable pyglet's events
    
    def __init__(self, planet):
        super( PanelLayer, self ).__init__()  
        
        self.currentPlanet = planet
        

        # add a label
        self.infoLabel = Label( "Debug Layer",
            font_size=12,
            multiline=True,
            height=director.get_window_size()[1] - 5,
            width=160,
            x=director.get_window_size()[0] - 5,
            y=director.get_window_size()[1] - 5,
            anchor_x=font.Text.RIGHT,
            anchor_y=font.Text.TOP )
        self.add( self.infoLabel )
        
        
        # add a label
        infoString = "\nPress ENTER on current planet (if yours) to enter planet screen\n"
        infoString += "\nPress B to build something random on current planet\n"
        infoString += "\nPress P to add people on current planet\n"
        infoString += "\nPress S to build a ship on current planet\n"
        infoString += "\nPress T to train a soldier on current planet\n"
        infoString += "\nDrag and drop to launch a ship\n"
        infoString += "\nPress L for a surprise\n"
        
        self.debugLabel = Label( infoString,
            font_size=11,
            multiline=True,
            color=(50,200,255,255),
            bold=True,
            height=director.get_window_size()[1] - 5,
            width=360,
            x=5,
            y=director.get_window_size()[1] - 5,
            anchor_x=font.Text.LEFT,
            anchor_y=font.Text.TOP )
        self.add( self.debugLabel )
        self.debugLabel.visible = False
        
        
    def on_enter(self):
        super( PanelLayer, self ).on_enter()   
        
        # shedule a function to update itself (the graphic) every frame
        self.schedule_interval(self.update,0.1)  
        
        
    def on_exit(self):
        super( PanelLayer, self ).on_exit()   
        
        
    def update(self,dt, *args, **kwargs):
        
        if self.currentPlanet != None:
            
            infoString = ""
            #infoString = "Planet - generated with seed " + str(self.seed) + "\n"
            infoString += "Name: " + str(self.currentPlanet.name) + "\n"
            infoString += "Owned by: " + OWNER_NAMES[self.currentPlanet.owner] + "\n"
            infoString += "Position: " + str(self.currentPlanet.position) + "\n"
            infoString += "Size: " + str(self.currentPlanet.size) + "\n"
            infoString += "Areas: " + str(len(self.currentPlanet.areas)) + "\n"
            infoString += "Temperature: " + str(self.currentPlanet.temperature) + "\n"
            infoString += "Type: " + str(PLANET_TYPE_NAMES[self.currentPlanet.type]) + "\n"
            infoString += "Population: " + str(self.currentPlanet.population) + "\n"
            infoString += "Minerals: " + str(self.currentPlanet.minerals) + "\n"
            infoString += "Organics: " + str(self.currentPlanet.organics) + "\n"
            infoString += "Soldiers: " + str(len(self.currentPlanet.soldiers)) + "\n"
            infoString += "Ships: " + str(len(self.currentPlanet.dockedShips)) + "\n"
            infoString += "Houses: " + str(self.currentPlanet.housesCount) + "\n"
            infoString += "Farms: " + str(self.currentPlanet.farmsCount) + "\n"
            infoString += "Mines: " + str(self.currentPlanet.minesCount) + "\n"
            if self.currentPlanet.soldiers:
                for s in self.currentPlanet.soldiers:
                    infoString += "Soldier " + str(s.name) + " | power: "+ str(s.power) + "\n"

            self.infoLabel.element.text = infoString
            
          
    def on_key_press( self, k , m ):
                    
        if k == key.F1:
            self.debugLabel.visible = not self.debugLabel.visible
